package com.duoec.dw.lineage.service.impl.analyser.statement;

import com.alibaba.druid.sql.ast.SQLStatement;
import com.duoec.dw.lineage.core.conf.MySqlAnalyserConfig;
import com.duoec.dw.lineage.dto.DdlChangeDto;
import com.duoec.dw.lineage.service.StatementAnalyser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

/**
 * @author xuwenzhen
 */
public abstract class BaseStatementAnalyser<T extends SQLStatement, D> implements StatementAnalyser<T, D> {
    @Autowired
    protected MySqlAnalyserConfig mySqlAnalyserConfig;

    public void setMySqlAnalyserConfig(MySqlAnalyserConfig mySqlAnalyserConfig) {
        this.mySqlAnalyserConfig = mySqlAnalyserConfig;
    }

    /**
     * 处理解析结果
     *
     * @param dto    DdlChangeDto实例
     * @param result 语句的处理结果
     */
    @Override
    public void setResult(DdlChangeDto dto, D result) {
        if (result instanceof DdlChangeDto) {
            DdlChangeDto ddlChangeDto = (DdlChangeDto) result;
            dto.setTableName(ddlChangeDto.getTableName());

            if (!CollectionUtils.isEmpty(ddlChangeDto.getItems())) {
                dto.getItems().addAll(ddlChangeDto.getItems());
            }
        }
    }
}
